﻿/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;
using NTasking.Internals;

namespace NTasking
{

    /// <summary>
    /// Represent a synchronized method family with 0 parameters and a specific return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="RetT">The return type</typeparam>
    public sealed class FuncFamily<IndexT, RetT> : MethodFamily<
        IndexT,
        RetT,
        Func<RetT>,
        Func<MethodCall<RetT>>,
        Func<IndexT, RetT>>
    {

        internal FuncFamily() { }


        protected override Func<RetT> CreateMemberDelegate(IndexT index)
        {
            return new FuncFamily<IndexT, RetT>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Func<RetT> SynchronousCall()
        {
            return () => Owner.SynchronousCall<RetT, Func<IndexT, RetT>>(Intercept());
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<MethodCall<RetT>> Intercept
        {
            get { return () => new MethodCall<IndexT, RetT>(Family, new CallContext<RetT>(), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override RetT Execute(CallContext context, Func<IndexT, RetT> body)
        {
            return body(Index);
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 1 parameters and a specific return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="RetT">The return type</typeparam>
    public sealed class FuncFamily<IndexT, P1T, RetT> : MethodFamily<
        IndexT,
        RetT,
        Func<P1T, RetT>,
        Func<P1T, MethodCall<RetT>>,
        Func<IndexT, P1T, RetT>>
    {

        internal FuncFamily() { }


        protected override Func<P1T, RetT> CreateMemberDelegate(IndexT index)
        {
            return new FuncFamily<IndexT, P1T, RetT>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Func<P1T, RetT> SynchronousCall()
        {
            return (p1) => Owner.SynchronousCall<RetT, Func<IndexT, P1T, RetT>>(Intercept(p1));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, MethodCall<RetT>> Intercept
        {
            get { return (p1) => new MethodCall<IndexT, RetT>(Family, new CallContext<P1T, RetT>(p1), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override RetT Execute(CallContext context, Func<IndexT, P1T, RetT> body)
        {
            IParameters<P1T> p = (IParameters<P1T>)context;

            return body(Index, p.P1);
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 2 parameters and a specific return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    /// <typeparam name="RetT">The return type</typeparam>
    public sealed class FuncFamily<IndexT, P1T, P2T, RetT> : MethodFamily<
        IndexT,
        RetT,
        Func<P1T, P2T, RetT>,
        Func<P1T, P2T, MethodCall<RetT>>,
        Func<IndexT, P1T, P2T, RetT>>
    {

        internal FuncFamily() { }


        protected override Func<P1T, P2T, RetT> CreateMemberDelegate(IndexT index)
        {
            return new FuncFamily<IndexT, P1T, P2T, RetT>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Func<P1T, P2T, RetT> SynchronousCall()
        {
            return (p1, p2) => Owner.SynchronousCall<RetT, Func<IndexT, P1T, P2T, RetT>>(Intercept(p1, p2));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, MethodCall<RetT>> Intercept
        {
            get { return (p1, p2) => new MethodCall<IndexT, RetT>(Family, new CallContext<P1T, P2T, RetT>(p1, p2), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override RetT Execute(CallContext context, Func<IndexT, P1T, P2T, RetT> body)
        {
            IParameters<P1T, P2T> p = (IParameters<P1T, P2T>)context;

            return body(Index, p.P1, p.P2);
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 3 parameters and a specific return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    /// <typeparam name="P3T">The type of the third parameter</typeparam>
    /// <typeparam name="RetT">The return type</typeparam>
    public sealed class FuncFamily<IndexT, P1T, P2T, P3T, RetT> : MethodFamily<
        IndexT,
        RetT,
        Func<P1T, P2T, P3T, RetT>,
        Func<P1T, P2T, P3T, MethodCall<RetT>>,
        Func<IndexT, P1T, P2T, P3T, RetT>>
    {

        internal FuncFamily() { }


        protected override Func<P1T, P2T, P3T, RetT> CreateMemberDelegate(IndexT index)
        {
            return new FuncFamily<IndexT, P1T, P2T, P3T, RetT>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Func<P1T, P2T, P3T, RetT> SynchronousCall()
        {
            return (p1, p2, p3) => Owner.SynchronousCall<RetT, Func<IndexT, P1T, P2T, P3T, RetT>>(Intercept(p1, p2, p3));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, P3T, MethodCall<RetT>> Intercept
        {
            get { return (p1, p2, p3) => new MethodCall<IndexT, RetT>(Family, new CallContext<P1T, P2T, P3T, RetT>(p1, p2, p3), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override RetT Execute(CallContext context, Func<IndexT, P1T, P2T, P3T, RetT> body)
        {
            IParameters<P1T, P2T, P3T> p = (IParameters<P1T, P2T, P3T>)context;

            return body(Index, p.P1, p.P2, p.P3);
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 4 parameters and a specific return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    /// <typeparam name="P3T">The type of the third parameter</typeparam>
    /// <typeparam name="P4T">The type of the fourth</typeparam>
    /// <typeparam name="RetT">The return type</typeparam>
    public sealed class FuncFamily<IndexT, P1T, P2T, P3T, P4T, RetT> : MethodFamily<
        IndexT,
        RetT,
        Func<P1T, P2T, P3T, P4T, RetT>,
        Func<P1T, P2T, P3T, P4T, MethodCall<RetT>>,
        Func<IndexT, P1T, P2T, P3T, P4T, RetT>>
    {

        internal FuncFamily() { }


        protected override Func<P1T, P2T, P3T, P4T, RetT> CreateMemberDelegate(IndexT index)
        {
            return new FuncFamily<IndexT, P1T, P2T, P3T, P4T, RetT>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Func<P1T, P2T, P3T, P4T, RetT> SynchronousCall()
        {
            return (p1, p2, p3, p4) => Owner.SynchronousCall<RetT, Func<IndexT, P1T, P2T, P3T, P4T, RetT>>(Intercept(p1, p2, p3, p4));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, P3T, P4T, MethodCall<RetT>> Intercept
        {
            get { return (p1, p2, p3, p4) => new MethodCall<IndexT, RetT>(Family, new CallContext<P1T, P2T, P3T, P4T, RetT>(p1, p2, p3, p4), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override RetT Execute(CallContext context, Func<IndexT, P1T, P2T, P3T, P4T, RetT> body)
        {
            IParameters<P1T, P2T, P3T, P4T> p = (IParameters<P1T, P2T, P3T, P4T>)context;

            return body(Index, p.P1, p.P2, p.P3, p.P4);
        }

    }

}
